package com.kgc.springboot.hangduo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.mapper.ClauseMapper;
import com.kgc.springboot.hangduo.pojo.Clause;
import com.kgc.springboot.hangduo.pojo.ClauseExample;
import com.kgc.springboot.hangduo.service.ClauseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: CBH
 * @date: 2020/12/27 14:28
 * @description:
 */
@Service
public class ClauseServiceImpl implements ClauseService {
    @Autowired
    private ClauseMapper clauseMapper;

    @Override
    public PageInfo<Clause> selectAll() {
        ClauseExample clauseExample = new ClauseExample();
        clauseExample.createCriteria();
        //clauseMapper.selectByExample();
        return null;
    }

    @Override
    public List<Clause> selectByCondition(String condition) {
        ClauseExample example = new ClauseExample();
        if (condition!=null && condition.length()>0){
            example.createCriteria().andClauseLike("%"+condition+"%");
        }
        List<Clause> clauses = clauseMapper.selectByExample(example);
        if (clauses!=null && clauses.size()>0){
            return clauses;
        }
        return null;
    }

    @Override
    public PageInfo<Clause> searchByCondition(Integer lawId, String clauseNum, String keyWord, Integer pageNum, Integer pageSize) {
        ClauseExample example = new ClauseExample();
        ClauseExample.Criteria criteria = example.createCriteria();
        if (lawId != null){
            criteria.andLawsIdEqualTo(lawId);
        }
        if (clauseNum != null && clauseNum.length()>0){
            criteria.andClauseLike("%"+clauseNum+"%");
        }
        if (keyWord != null && keyWord.length()>0){
            criteria.andTitelLike("%"+keyWord+"%");
        }

        PageHelper.startPage(pageNum,pageSize);

        List<Clause> clauses = clauseMapper.selectByExample(example);

        PageInfo<Clause> pageInfo = new PageInfo<>(clauses);
        return pageInfo;
    }

    @Override
    public Integer insertClause(Clause clause) {
        if (clause!=null){
            int res = clauseMapper.insertSelective(clause);
            return res;
        }
        return null;
    }

    @Override
    public Integer updateClauseSelective(Clause clause) {
        if (clause!=null && clause.getId()!=null){
            int res = clauseMapper.updateByPrimaryKeySelective(clause);
            return res;
        }
        return null;
    }

    @Override
    public Integer delClauseById(Integer id) {
        if (id!=null){
            int res = clauseMapper.deleteByPrimaryKey(id);
            return res;
        }
        return null;
    }

    @Override
    public Clause selectById(Integer id) {
        Clause clause = clauseMapper.selectByPrimaryKey(id);
        return clause;
    }
}
